﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;

namespace RandomOrderedTest
{
    class Program
    {
        private static int randomCount = 10;

        private static string originalOrderdTestPath = string.Empty;

        static void Main(string[] args)
        {
            try
            {
                // Init args
                //originalOrderdTestPath = args[0];
                //randomCount = int.Parse(args[1]);

                originalOrderdTestPath = @"D:\SDClient\OFFICE_DEV\TestSuites\Office\MS-FSSHTTP\Plugfest\TestCode\Server\TestSuite\OrderedTest1VS2008.orderedtest";
                
                XmlDocument doc = new XmlDocument();
                doc.Load(originalOrderdTestPath);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.DocumentElement.OwnerDocument.NameTable);
                nsmgr.AddNamespace("nsTest", "http://microsoft.com/schemas/VisualStudio/TeamTest/2006");
                
                XmlNodeList testLinkItems = doc.SelectNodes("//nsTest:TestLink", nsmgr);
                if (testLinkItems == null || testLinkItems.Count == 0)
                {
                    return;
                }

                List<string> links = new List<string>(testLinkItems.Count);
                foreach (XmlNode linkNode in testLinkItems)
                {
                    links.Add(linkNode.OuterXml);
                }

                XmlNode testLinks = doc.SelectSingleNode("//nsTest:TestLinks", nsmgr);
                testLinks.InnerXml = string.Empty;

                List<string> randomedList = RandomList(links);
                foreach (string listItem in randomedList)
                {
                    testLinks.InnerXml += listItem;
                }

                doc.Save(originalOrderdTestPath);

                //GenerateOrderedTest();
            }
            catch (Exception e)
            {
                Console.WriteLine("Program failure because \"" + e.Message + " \"");
                Console.WriteLine();
                Console.WriteLine();
                Usage();
            }
        }

        private static string GetNamespace(string fullSchema)
        {
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(fullSchema);

            if (xd.ChildNodes.Count <= 1)
            {
                throw new Exception("Can't find second childNode in " + xd.Name);
            }

            XmlAttributeCollection attributeList = xd.ChildNodes[1].Attributes;
            foreach (XmlAttribute attribute in attributeList)
            {
                if (attribute.Name == "targetNamespace")
                {
                    return attribute.Value;
                }
            }

            return string.Empty;
        }

        private static List<string> RandomList(List<string> links)
        {
            List<string> randomedList = new List<string>();

            Random ran = new Random();
            int num = 0;
            while ((num = ran.Next(0, links.Count)) > -1 )
            {
                if (num == links.Count)
                {
                    continue;
                }

                string item = links[num];
                if (!randomedList.Contains(item))
                {
                    randomedList.Add(item);
                }

                if (randomedList.Count == links.Count)
                {
                    break;
                }
            }

            return randomedList;
        }

        private static int InitArgs(string[] args)
        {
            for (int currentIndex = 0; currentIndex < args.Length; currentIndex++)
            {
                string currentOption = args[currentIndex].ToLower();
                if (currentOption.StartsWith("-") || currentOption.StartsWith("/"))
                {
                    currentOption = currentOption.Remove(0, 1);
                }

                switch (currentOption)
                {
                    case "help":
                    case "?":
                        Usage();
                        System.Environment.Exit(0);
                        break;

                    default:
                        return currentIndex;
                }
            }

            Usage();
            System.Environment.Exit(0);
            return args.Length;
        }

        private static void Usage()
        {
            Console.WriteLine("RandomOrderedTest.exe [options,] file, number");
            Console.WriteLine();
            Console.WriteLine("  Options:");
            Console.WriteLine("     -help, -?    Show help information");
            Console.WriteLine();
            Console.WriteLine(" General information:");
            Console.WriteLine("   RandomOrderedTest will create .orderedtest files based on input parameters.");
            Console.WriteLine();
        }
    }
}
